﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class BomBLL : IBaseInfoDetail
    {
        private static readonly IBomDA bomDA = DataAccess.CreateBomDA();
        private static readonly IBomDetailDA bomDetailDA = DataAccess.CreateBomDetailDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IOwnerDA OwnerDA = DataAccess.CreateOwnerDA();
        private PubParasInfo pubParasInfo;
        private static readonly ISkuDA SkuDA = DataAccess.CreateSkuDA();

        public BomBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public BomBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteDetail(List<string> OwnerID, List<string> SkuID, List<string> pk3)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                List<BomInfo> bomInfos = new List<BomInfo>();
                BomInfo bomInfo = null;
                for (i = 0; i < OwnerID.Count; i++)
                {
                    bomInfo = bomDA.GetBomByID(dataBase, tran, OwnerID[i], SkuID[i], this.dataBaseID[0]);
                    bomInfo.BomDetailInfo = bomDetailDA.GetBomDetailListByID(dataBase, tran, OwnerID[i], SkuID[i], this.dataBaseID[0]);
                    bomInfos.Add(bomInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                bomDetailDA.DeleteBomDetail(dataBase, tran, OwnerID, SkuID, pk3, this.dataBaseID);
                bomDA.DeleteBom(dataBase, tran, OwnerID, SkuID, this.dataBaseID);
                for (i = 0; i < bomInfos.Count; i++)
                {
                    auditDataBLL.InsertAuditData<BomInfo>(dataBase, tran, bomInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "BOM", this.dataBaseID[0]);
                    if ((bomInfos[i].BomDetailInfo != null) && (bomInfos[i].BomDetailInfo.Count > 0))
                    {
                        for (int j = 0; j < bomInfos[i].BomDetailInfo.Count; j++)
                        {
                            auditDataBLL.InsertAuditData<BomDetailInfo>(dataBase, tran, bomInfos[i].BomDetailInfo[j], null, AuditAction.Delete, this.pubParasInfo.UserID, "BOM_DETAIL", this.dataBaseID[0]);
                        }
                    }
                    string CS00002 = temp;
                    temp = CS00002 + bomInfos[i].OwnerID + "," + bomInfos[i].SkuID + ";";
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllDetailInfo()
        {
            return bomDA.GetAllBom(this.dataBaseID[0]);
        }

        public DataSet GetDetaiInfolByQueryEntity(object obj)
        {
            BomQueryEntity bomQuery = obj as BomQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(bomQuery.OwnerID))
            {
                sqlWhere.Add(" OWNER_ID " + bomQuery.OwnerID);
            }
            if (!string.IsNullOrEmpty(bomQuery.SkuID))
            {
                sqlWhere.Add(" SKU_ID " + bomQuery.SkuID);
            }
            if (!string.IsNullOrEmpty(bomQuery.Remark))
            {
                sqlWhere.Add(" REMARK " + bomQuery.Remark);
            }
            if (!string.IsNullOrEmpty(bomQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + bomQuery.IsActive);
            }
            return bomDA.GetBomByQueryList(sqlWhere, bomQuery, this.dataBaseID[0]);
        }

        public object GetDetailInfoByID(string OwnerID, string SkuID, string pk3)
        {
            BomInfo bomInfo = bomDA.GetBomByID(OwnerID, SkuID, this.dataBaseID[0]);
            if (bomInfo != null)
            {
                bomInfo.BomDetailInfo = bomDetailDA.GetBomDetailListByID(OwnerID, SkuID, this.dataBaseID[0]);
            }
            return bomInfo;
        }

        public List<object> GetDetailInfoListByID(string mainID, string subID)
        {
            throw new NotImplementedException();
        }

        public string InsertDetail(object obj)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string bomID = string.Empty;
            try
            {
                BomInfo bom = obj as BomInfo;
                if (bomDA.CheckBomIDUnique(dataBase, tran, bom.OwnerID, bom.SkuID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2270_001", this.pubParasInfo.UserLanguage, new List<string> { bom.OwnerID, bom.SkuID });
                }
                if (OwnerDA.GetTradePartnerByID(bom.OwnerID, this.dataBaseID[0]) == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2270_002", this.pubParasInfo.UserLanguage, new List<string> { bom.OwnerID });
                }
                if (SkuDA.GetSkuByID(bom.OwnerID, bom.SkuID, this.dataBaseID[0]) == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2270_003", this.pubParasInfo.UserLanguage, new List<string> { bom.SkuID });
                }
                bom.UpdatedBy = bom.CreatedBy;
                bom.CreatedDate = DateTime.Now;
                bom.UpdatedDate = DateTime.Now;
                bomDA.InsertBom(dataBase, tran, bom, this.dataBaseID);
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                auditDataBLL.InsertAuditData<BomInfo>(dataBase, tran, null, bom, AuditAction.Insert, this.pubParasInfo.UserID, "BOM", this.dataBaseID[0]);
                bomID = bom.OwnerID + "," + bom.SkuID;
                if ((bom.BomDetailInfo != null) && (bom.BomDetailInfo.Count > 0))
                {
                    for (int i = 0; i < bom.BomDetailInfo.Count; i++)
                    {
                        if (bomDetailDA.CheckBomDetailIDUnique(dataBase, tran, bom.BomDetailInfo[i].OwnerID, bom.BomDetailInfo[i].SkuID, bom.BomDetailInfo[i].ComponentSkuID, this.dataBaseID[0]))
                        {
                            PublicMethod.GetInstance().ShowAlertMessage("F2270_004", this.pubParasInfo.UserLanguage, new List<string> { bom.BomDetailInfo[i].ComponentSkuID });
                        }
                        else
                        {
                            bom.BomDetailInfo[i].CreatedBy = this.pubParasInfo.UserID;
                            bom.BomDetailInfo[i].UpdatedBy = this.pubParasInfo.UserID;
                            bom.BomDetailInfo[i].CreatedDate = DateTime.Now;
                            bom.BomDetailInfo[i].UpdatedDate = DateTime.Now;
                            bomDetailDA.InsertBomDetail(dataBase, tran, bom.BomDetailInfo[i], this.dataBaseID);
                            auditDataBLL.InsertAuditData<BomDetailInfo>(dataBase, tran, null, bom.BomDetailInfo[i], AuditAction.Insert, this.pubParasInfo.UserID, "BOM", this.dataBaseID);
                        }
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return bomID;
        }

        public string UpdateDetail(object obj)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string bomID = string.Empty;
            try
            {
                BomInfo bom = obj as BomInfo;
                bom.UpdatedDate = DateTime.Now;
                bom.UpdatedBy = this.pubParasInfo.UserID;
                BomInfo oldBomInfo = bomDA.GetBomByID(dataBase, tran, bom.OwnerID, bom.SkuID, this.dataBaseID[0]);
                if (bomDA.UpdateBom(bom, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<BomInfo>(dataBase, tran, oldBomInfo, bom, AuditAction.Update, this.pubParasInfo.UserID, "BOM", this.dataBaseID);
                    bomID = bom.OwnerID + "," + bom.SkuID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return bomID;
        }

        public string UpdateIsActive(List<string> mainID, List<string> subId, string isActive)
        {
            throw new NotImplementedException();
        }
    }
}

